package point24.controller;

import org.apache.ibatis.session.SqlSession;
import point24.model.GameModel;
import point24.pojo.Point24;
import point24.pojo.Point24DAO;
import point24.view.GameView;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.Vector;

public class GameController extends GameView implements ActionListener {
    public static final int BOUND = 13;
    GameModel gameModel = new GameModel();
    Random random = new Random();
    Point24DAO dao = Point24DAO.getInstance();
    SqlSession session = dao.getSession();

    public GameController() {
        super();
        Integer[] arr = new Integer[4];
        for (int i = 0; i < 4; i++) {
            arr[i] = random.nextInt(BOUND) + 1;
        }
        Arrays.sort(arr);
        gameModel.setNumbers(arr);
        super.jTextFieldA.setText(String.format("%d", arr[0]));
        super.jTextFieldB.setText(String.format("%d", arr[1]));
        super.jTextFieldC.setText(String.format("%d", arr[2]));
        super.jTextFieldD.setText(String.format("%d", arr[3]));
        super.jButtonAbout.addActionListener(this);
        super.jButtonInput.addActionListener(this);
        super.jButtonRand.addActionListener(this);
        super.jButtonSolve.addActionListener(this);
        super.jButtonSave.addActionListener(this);
        super.jButtonQuery.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == super.jButtonRand) {
            super.jLabelHint.setText("随机产生一组数据");
            Integer[] arr = new Integer[4];
            for (int i = 0; i < 4; i++) {
                arr[i] = random.nextInt(BOUND) + 1;
            }
            Arrays.sort(arr);
            gameModel.setNumbers(arr);
            super.jTextFieldA.setText(String.format("%d", arr[0]));
            super.jTextFieldB.setText(String.format("%d", arr[1]));
            super.jTextFieldC.setText(String.format("%d", arr[2]));
            super.jTextFieldD.setText(String.format("%d", arr[3]));

        } else if (e.getSource() == super.jButtonAbout) {
            super.jLabelHint.setText("关于项目");
            JOptionPane.showMessageDialog(null, "欢乐24点小游戏，2023.6.28~7.9 @安徽工业大学");
        } else if (e.getSource() == super.jButtonInput) {
            Integer[] arr = new Integer[4];
            arr[0] = Integer.parseInt(super.jTextFieldA.getText());
            arr[1] = Integer.parseInt(super.jTextFieldB.getText());
            arr[2] = Integer.parseInt(super.jTextFieldC.getText());
            arr[3] = Integer.parseInt(super.jTextFieldD.getText());
            Arrays.sort(arr);
            gameModel.setNumbers(arr);
            super.jLabelHint.setText("用户输入数据完毕");
        } else if (e.getSource() == super.jButtonSolve) {
            super.jLabelHint.setText("开始求解");
            gameModel.calculate();
            super.jListResult.setListData(new Vector(gameModel.getResult()));
            super.jLabelHint.setText(String.format("AI共找到%d组解答", gameModel.getResult().size()));

        } else if (e.getSource() == super.jButtonSave) {
            super.jLabelHint.setText("点击保存数据");
            int cnt = 0;
            if (!gameModel.getResult().isEmpty()) {
                List<Point24> dbList = session.selectList("listPoint24ByNumbers", Arrays.asList(gameModel.getNumbers()).toString());
                List<Point24> allList = new Vector<>();
                for (String result : gameModel.getResult()) {
                    Point24 point24 = new Point24();
                    point24.setNumbers(Arrays.asList(gameModel.getNumbers()).toString());
                    point24.setSolution(result);
                    dbList.add(point24);
                }
                allList.removeAll(dbList);//去除数据库里的已有记录
                for (Point24 point24 : allList) {
                    session.insert("addPoint24", point24);
                    ++cnt;
                }
            }
            if (cnt > 0) {
                session.commit();
                super.jLabelHint.setText("插入数据成功,共插入" + cnt + "条");
            } else {
                super.jLabelHint.setText("插入数据失败");
            }
        } else if (e.getSource() == super.jButtonQuery) {
            super.jLabelHint.setText("点击查询数据");
            List<Point24> lists = session.selectList("listPoint24ByNumbers", Arrays.asList(gameModel.getNumbers()).toString());
            Vector<String> resultList = new Vector<>();
            for (Point24 point24 : lists) {
                resultList.add(point24.getSolution());
            }

            super.jListResult.setListData(resultList);
            super.jLabelHint.setText("查询数据成功,共" + resultList.size() + "条");


        }
    }
}
